package com.baili.healthcommoncore.domain.handler;

/**
 * @Author：ShenJiaFeng
 * @Package：com.baili.healthcommoncore.domain.handler
 * @Project：baili-health-cloud
 * @name：GlobalExceptionHandler
 * @Date：2024/5/24 21:20
 */

import com.baili.healthcommoncore.domain.result.Result;
import lombok.extern.log4j.Log4j2;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常定义
 */
@Log4j2
@RestControllerAdvice
@Configuration
public class GlobalExceptionHandler {
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public Result<String> runtimeException (MethodArgumentNotValidException exception) {
//        log.error("请求异常：[{}]", exception.getMessage(), exception);
//        return Result.error(
//                JSONObject.toJSONString(
//                        exception.getBindingResult().getAllErrors()
//                                .stream()
//                                .map(ObjectError::getDefaultMessage)
//                                .toList()
//                )
//        );
        return Result.error();
    }

    @ExceptionHandler(value = IllegalArgumentException.class)
    public Result<String> illegalArgumentExceptionHandler(IllegalArgumentException exception){
        log.error("请求异常：[{}]", exception.getMessage(), exception);
        return Result.error(exception.getMessage());
    }


}
